import { ref, reactive, computed } from 'vue'
import { defineStore } from 'pinia'
import { doLoginAPI } from "@/api/user"

export const useAuthStore = defineStore('auth', () => {
  const auth = reactive({
    userid: null,
    token: null,
  })

  const isLogin = computed(() => {
    return !!(auth.userid && auth.token);
  })

  const exitAndUpdateAuth = ()=>{
    auth.userid = null;
    auth.token = null;
  }

  const loginAndUpdateAuth = async (data: LoginParamsRaw) => {
    try {
        let result = await doLoginAPI(data);
        let {userid,token} = result.data;
        auth.userid = userid;
        auth.token = token;
    } catch (err: any) {
       return Promise.reject(err);
    } 
  }

  return { auth,isLogin, loginAndUpdateAuth,exitAndUpdateAuth}
},{
  persist:true,
  // persist:{
  //   // key:"lw-auth",
  //   // paths: ['auth.token'],
  // },
})
